//
//  TouchViewController.m
//  UIComprehensive
//
//  Created by lanou3g on 16/1/26.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "TouchViewController.h"
#import "TouchView.h"
@interface TouchViewController ()
@property(nonatomic,strong)TouchView *touchView;
@property (nonatomic,strong )UIView *gustureView;
@property (nonatomic,strong )UITextField *textField;
@end

@implementation TouchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"touch事件";
    [self touchInit];
    [self gustureViewInit];
    [self addGusture];
}
#pragma mark - 懒加载
-(UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 450, 200, 50)];
        [self.view addSubview:_textField];
    }
    return _textField;
}
#pragma mark - 触摸事件的初始化
- (void)gustureViewInit{
    self.gustureView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300, 400)];
    self.gustureView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.gustureView];
}
#pragma mark - 添加手势
- (void)addGusture{
    //第一种手势：轻点
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//    tap.numberOfTapsRequired = 2;//双击触发
//    tap.numberOfTouchesRequired = 2;//双指点击
    [self.gustureView addGestureRecognizer:tap];
    //第二种：长摁
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    [self.gustureView addGestureRecognizer:longPress];
    //第三种手势：旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [self.gustureView addGestureRecognizer:rotation];
    //第四种手势: 捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self.gustureView addGestureRecognizer:pinch];
    //第五种手势：清扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    //清扫方向（枚举）
    swipe.direction = UISwipeGestureRecognizerDirectionUp;
    [self.gustureView addGestureRecognizer:swipe];
    //第六种：平移
    UIPanGestureRecognizer *pan= [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.gustureView addGestureRecognizer:pan];
    //第七种：从边缘
    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgePanAction:)];
    [self.gustureView addGestureRecognizer:edgePan];
}
#pragma mark - 文本框显示手势类型
- (void)tapAction:(UITapGestureRecognizer *)sender{
    self.textField.text = @"点击";
}
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{
    self.textField.text = @"长按";
}
- (void)swipeAction:(UISwipeGestureRecognizer *)sender{
    self.textField.text = @"清扫";
}
#pragma mark - Transform属性实现图形旋转、捏合、平移
- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
    self.textField.text = @"旋转";
    
    //旋转
    self.gustureView.transform = CGAffineTransformRotate(self.gustureView.transform, sender.rotation);
    //每次旋转后将旋转角度置0，避免影响下一次旋转
    sender.rotation = 0 ;
    
}
- (void)pinchAction:(UIPinchGestureRecognizer *)sender{
    self.textField.text = @"捏合";
    
    //缩放属性
    self.gustureView.transform = CGAffineTransformScale(self.gustureView.transform, sender.scale, sender.scale);
    //每次缩放后将缩放比例还原为1，避免影响下一次缩放
    sender.scale = 1;
}

- (void)panAction:(UIPanGestureRecognizer *)sender{
    self.textField.text = @"平移";
    
    //获得平移点的信息（与touch...move方法中前后两点的距离类似）
    CGPoint point = [sender translationInView:self.gustureView];
    //通过设置gustureView的属性transform属性来实现平移
    self.gustureView.transform = CGAffineTransformTranslate(self.gustureView.transform, point.x, point.y);
    //每次平移后清空之前的纪录
    [sender setTranslation:CGPointZero inView:self.gustureView];
    
}
- (void)edgePanAction:(UIScreenEdgePanGestureRecognizer *)sender{
    self.textField.text = @"边缘平移";
}
#pragma mark - 触摸事件的初始化
//圆跟随鼠标移动
- (void)touchInit{
    self.touchView = [[TouchView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - 100, 300, 200, 200)];
    self.touchView.layer.cornerRadius = 100;
    self.touchView.layer.borderWidth = 3;
    self.touchView.layer.backgroundColor = [UIColor blueColor].CGColor;
    [self.view addSubview:self.touchView];
}

#pragma mark - 触摸事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸开始调用的方法");
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸事件被优先级更高的事件打断时出发的事件");
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"移动触摸时调用的方法");
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"触摸结束调用的方法");
}
#pragma mark - 晃动事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"开始晃动时触发的事件");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"晃动被优先级更高的事件打断时调用的方法");
}
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event{
    NSLog(@"晃动事件结束时出发的事件");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
